package com.inhim.pjhim.adapter.provider;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chaychan.adapter.BaseItemProvider;
import com.example.bannerview.adapter.ViewHolder;
import com.example.bannerview.banner_view.BannerAdapter;
import com.example.bannerview.banner_view.BannerView;
import com.example.bannerview.banner_view.indicator.Indicator;
import com.example.bannerview.banner_view.indicator.RectIndicator;
import com.inhim.pjhim.R;
import com.inhim.pjhim.activity.AlllCateActivity;
import com.inhim.pjhim.activity.BannerWebViewActivity;
import com.inhim.pjhim.activity.VideoActivity;
import com.inhim.pjhim.adapter.ReadingTwoAdapter;
import com.inhim.pjhim.app.MyApplication;
import com.inhim.pjhim.entity.BannerList;
import com.inhim.pjhim.view.CustomRoundAngleImageView;
import com.zhouwei.mzbanner.holder.MZViewHolder;
import java.util.List;

/**
 * @author ChayChan
 * @description: 头部banner
 * @date 2018/3/22  14:48
 */

public class BannerItemProvider extends BaseItemProvider<BannerList, BaseViewHolder> {
    private Context context;
    private BannerAdapter<BannerList.Data> mBannerAdapter;
    private RectIndicator mCircleIndicator;
    private String TAG="Home";
    public BannerItemProvider(Context context) {
        this.context=context;
    }

    @Override
    public void convert(BaseViewHolder helper, final BannerList news, int i) {
        if (news.getData() == null) {
            //如果没有标题，则直接跳过
            return;
        }
        try{
            //处理相关业务逻辑
            BannerView mMZBanner = helper.getView(R.id.bannerView);
            RadioButton radioButton1=helper.getView(R.id.radio1);
            RadioButton radioButton2=helper.getView(R.id.radio2);
            RadioButton radioButton3=helper.getView(R.id.radio3);
            radioButton1.setOnClickListener(v -> startActivity(0,context.getResources().getString(R.string.video)));
            radioButton2.setOnClickListener(v -> startActivity(1,context.getResources().getString(R.string.listen_to_the)));
            radioButton3.setOnClickListener(v -> startActivity(2,context.getResources().getString(R.string.reading)));
            List<BannerList.Data> bannlist1 =news.getData();
            initAdapter(bannlist1);
            initIndicator();

            //设置适配器
            setAdapter(mBannerAdapter, mMZBanner);

            //设置指示器
            setIndicator(mCircleIndicator, mMZBanner);

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void startActivity(int Type,String Title){
        /*Intent intent=new Intent(context, TypeListActivity.class);
        intent.putExtra("TAG",TAG);
        intent.putExtra("Type",Type);
        intent.putExtra("Title",Title);*/
        Intent intent=new Intent(context, AlllCateActivity.class);
        intent.putExtra("position",Type);
        context.startActivity(intent);
    }

    private void initAdapter(List<BannerList.Data> bannerList) {
        mBannerAdapter = new BannerAdapter<BannerList.Data>(context, R.layout.item_3d_banner, bannerList) {
            @Override
            public void convert(ViewHolder holder, final BannerList.Data item) {
                holder.setImage(R.id.iv_image, item.getImgUrl());
                holder.itemView.setOnClickListener(v -> {
                    Intent intent;
                    if(item.getLinkType()==2 && "".equals(item.getLink())){
                        intent=new Intent(mContext, BannerWebViewActivity.class);
                        intent.putExtra("url",item.getLink()) ;
                        mContext.startActivity(intent);
                    }else{
                        if(item.getLink()==null || "".equals(item.getLink())){
                            return;
                        }
                        intent=new Intent(context, VideoActivity.class);
                        intent.putExtra("ReaderId",Integer.valueOf(item.getLink())) ;
                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                        mContext.startActivity(intent);
                    }
                });
            }

        };
    }

    private void initIndicator() {
        mCircleIndicator = new RectIndicator(context, RectIndicator.SHAPE_CIRCLE);
        //设置指示器长和宽.
        mCircleIndicator.setIndicatorSize(20, 10);
        //设置指示器之间的间距
        mCircleIndicator.setItemMargin(10);
        //设置指示器显示在banner的底部的中间
        mCircleIndicator.setRules(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.CENTER_HORIZONTAL);
        //设置指示器显示距离banner底部10dp
        mCircleIndicator.setMargins(0, 0, 0, 0);
        mCircleIndicator.setIndicatorDrawable(R.mipmap.indicator_focus,R.mipmap.indicator_normal);

    }

    private void setAdapter(BannerAdapter adapter, BannerView bannerView) {
        bannerView.setAdapter(adapter);
    }

    private void setIndicator(Indicator indicator, BannerView bannerView) {
        bannerView.addIndicator(indicator);
    }

    @Override
    public int viewType() {
        return ReadingTwoAdapter.BANNER;
    }

    @Override
    public int layout() {
        return R.layout.recyclerview_header;
    }

    class LocalImageHolderView implements MZViewHolder<BannerList.Data> {

        private CustomRoundAngleImageView imageView;

        @Override
        public View createView(Context context) {
            // 返回页面布局文件
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item,null);
            imageView = view.findViewById(R.id.banner_image);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            return view;
        }
        @Override
        public void onBind(Context context, int position, BannerList.Data data) {
            // 数据绑定
            Glide.with(MyApplication.getContext()).load(data.getImgUrl()).into(imageView);
        }
    }
}
